package com.xdf.bling.mybatis;

import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.exception.InvalidConfigurationException;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * mybatis生成
 * @author
 */
public class MyBatisGeneratorTool {
    public static void main(String[] args) {
        List<String> warnings = new ArrayList<String>();
        String genCfg = "build-mybatis.xml";
        System.out.println(MyBatisGeneratorTool.class.getResource("build-mybatis.xml"));
        //File configFile = new File(MyBatisGeneratorTool.class.getResource(genCfg).getPath().replace("%20", " "));
        File configFile = new File("src/main/java/com/xdf/bling/mybatis/build-mybatis.xml");
        ConfigurationParser cp = new ConfigurationParser(warnings);
        Configuration config = null;
        try {
            config = cp.parseConfiguration(configFile);
        } catch (IOException | XMLParserException e) {
            e.printStackTrace();
        }
        DefaultShellCallback callback = new DefaultShellCallback(true);
        MyBatisGenerator myBatisGenerator = null;
        try {
            if (config != null) {
                myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
            }
        } catch (InvalidConfigurationException e) {
            e.printStackTrace();
        }
        try {
            if (myBatisGenerator != null) {
                myBatisGenerator.generate(null);
            }
        } catch (SQLException | IOException | InterruptedException e) {
            e.printStackTrace();
        }

    }
}
